package com.flkin.modules.system.service.impl;

import com.flkin.modules.system.domain.Authority;
import com.flkin.modules.system.repository.AuthorityRepository;
import com.flkin.modules.system.service.AuthorityService;
import com.flkin.modules.system.service.dto.AuthorityDto;
import com.flkin.modules.system.service.mapstruct.AuthorityMapper;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Set;
import javax.annotation.Resource;

/**
 * @author yanxiang.huang 2020-12-07 22:32:08
 */
@Service
public class AuthorityServiceImpl implements AuthorityService {

    @Resource
    private AuthorityRepository authorityRepository;
    @Resource
    private AuthorityMapper authorityMapper;

    @Override
    public List<AuthorityDto> getAll() {
        return authorityMapper.toDto(authorityRepository.findAll());
    }

    @Override
    public AuthorityDto findById(Long id) {
        Authority authority = authorityRepository.findById(id).orElse(null);
        if (authority != null) {
            return authorityMapper.toDto(authority);
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AuthorityDto create(Authority resource) {
        return authorityMapper.toDto(authorityRepository.save(resource));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Authority resource) {
        authorityRepository.save(resource);
    }

    @Override
    public void deleteAll(List<Long> ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }

    @Override
    public void deleteById(Long id) {
        authorityRepository.deleteById(id);
    }

    @Override
    public Set<Authority> findByRoleId(Long id) {
        return authorityRepository.findByRoleId(id);
    }

}
